/*
 * AbstractSQLIndiceBursatilDAO.java
 *
 * Created on 31 de octubre de 2007, 16:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.indiceBursatil.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import webportlet.model.indiceBursatil.dto.IndiceBursatilDTO;
import webportlet.model.util.InstanceNotFoundException;
import webportlet.model.util.InternalErrorException;
import webportlet.model.util.*;
/**
 *
 * @author Administrador
 */
public abstract class AbstractSQLIndiceBursatilDAO implements SQLIndiceBursatilDAO  {
       
   
    public abstract IndiceBursatilDTO create(Connection connection, IndiceBursatilDTO indiceBursatilDTO) 
    throws DuplicateInstanceException, InternalErrorException;
    public boolean exits(Connection connection, Long id) 
        throws InternalErrorException
    {
     
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        
        
        try
        {
            String queryString = "SELECT idIndice FROM IndiceBursatil" + " WHERE idIndice = ?";
            
            preparedStatement = connection.prepareStatement(queryString);
            
            int i=1;
            
            preparedStatement.setLong(i++,id);
            
            resultSet =preparedStatement.executeQuery();
            
           return resultSet.next();
            
         }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
        
    }
    public IndiceBursatilDTO find(Connection connection, Long id)
     throws InstanceNotFoundException, InternalErrorException 
    {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        
        try
        {
            String queryString = "SELECT empresa, valor" + " FROM IndiceBursatil" + 
                    " WHERE idIndice=?";
            
            preparedStatement = connection.prepareStatement(queryString);
            int i=1;
            preparedStatement.setLong(i++,id);
            
            resultSet = preparedStatement.executeQuery();
            
            if (!resultSet.next()) {
               throw new InstanceNotFoundException(id,IndiceBursatilDTO.class.getName());
            }
            
            i=1;
            
            String empresa = resultSet.getString(i++);
            Double valor = resultSet.getDouble(i++);
            
            IndiceBursatilDTO indiceBursatilDTO = new IndiceBursatilDTO(id,empresa,valor);
            
            return indiceBursatilDTO;
            
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }    
        
        
    }
    public void update(Connection connection, IndiceBursatilDTO indiceBursatilDTO) 
        throws InstanceNotFoundException, InternalErrorException{
        
        PreparedStatement preparedStatement = null;
     
        try
        {
            String queryString = "UPDATE IndiceBursatil" + " SET empresa=?, valor = ?" +
                    " WHERE idIndice=?";
            
            preparedStatement = connection.prepareStatement(queryString);
            int i=1;
            
            
            preparedStatement.setString(i++,indiceBursatilDTO.getEmpresa());
            preparedStatement.setDouble(i++,indiceBursatilDTO.getValor());
            preparedStatement.setLong(i++,indiceBursatilDTO.getId());
            
            int updateRows = preparedStatement.executeUpdate();
            
            if (updateRows == 0) {
                throw new InstanceNotFoundException(indiceBursatilDTO.getId(), "IndiceBursatil");
            }

            if (updateRows > 1) {
                throw new SQLException("Nombres duplicados para la fila = '" + 
                    indiceBursatilDTO.getId() + "' in table 'IndiceBursatil'");
            }        
            
            
        }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }           
       
        
        
    }
    public void remove(Connection connection, Long id)
         throws InstanceNotFoundException, InternalErrorException
    {
         PreparedStatement preparedStatement = null;
         
         try
         {
            String queryString = "DELETE FROM IndiceBursatil WHERE" +
                " idIndice = ?";
            
            
            preparedStatement = connection.prepareStatement(queryString);
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++,id);
            
            int removedRows = preparedStatement.executeUpdate();
            
            if(removedRows==0)
            {
                throw new InstanceNotFoundException(id, IndiceBursatilDTO.class.getName());
            }
          }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }    
    
        
    
    }
    public ArrayList <IndiceBursatilDTO> recuperarListaCompleta(Connection connection)
            throws InstanceNotFoundException, InternalErrorException{
        
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        ArrayList <IndiceBursatilDTO> listaIndicesBursatiles = new ArrayList();
        
        try
        {
            String queryString = "SELECT idIndice, empresa, valor FROM IndiceBursatil";
            
            preparedStatement = connection.prepareStatement(queryString);
            
            resultSet = preparedStatement.executeQuery();
            
            while(resultSet.next()) {
                Long id = resultSet.getLong("idIndice");
                String empresa = resultSet.getString("empresa");
                Double valor = resultSet.getDouble("valor");
                IndiceBursatilDTO indiceAux = new IndiceBursatilDTO(id,empresa,valor);
                listaIndicesBursatiles.add(indiceAux);
            }
            
            return listaIndicesBursatiles;
            
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }
     public ArrayList <IndiceBursatilDTO> recuperarLista(Connection connection, 
            ArrayList <Long> listaIdentificadores)
            throws InstanceNotFoundException, InternalErrorException{
        
        ArrayList <IndiceBursatilDTO> listaIndicesBursatiles = new ArrayList();
        for(int i=0; i<listaIdentificadores.size(); i++){
            
            if(!listaIndicesBursatiles.add(this.find(connection,
                    listaIdentificadores.get(i)))){
                throw new InstanceNotFoundException(listaIdentificadores.get(i),
                    IndiceBursatilDTO.class.getName());
            }
        }
        return listaIndicesBursatiles;
    }
 
       
}
